package org.neogenesis.web.loader;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.neogenesis.web.schema.TableType;
import org.neogenesis.web.schema.TableType.Column;

public class ProbesLoaderGeoSeries extends ProbesLoader{

	private List<Column> columns;
	
	private Column idColumn;

	public ProbesLoaderGeoSeries(String filePath, List<Column> columns)
			throws IOException {
		this.columns = columns;
		
		this.rows = new LinkedList<String[]>(); 

		defineFileType();

		BufferedReader in = new BufferedReader(new FileReader(filePath));

		String line = null;

		while ((line = in.readLine()) != null) {
			String[] split = line.split("\t");
			
			if (split.length < (idColumn.getPosition().intValue() -1))
				continue;
			
			if (split[(idColumn.getPosition().intValue() -1)].equalsIgnoreCase("---"))
				continue;
			
			rows.add(split);
		}
		in.close();
	}

	@Override
	protected void defineFileType() {
		
		typeMetaPlatform = TypeMetaPlatform.NONE;
		TypeMetaPlatform temp;
		
		for (Column column : columns) {
			try {				
				temp = TypeMetaPlatform.valueOf(column.getName()
						.toUpperCase());
				if (temp.priority() > typeMetaPlatform.priority()){
					typeMetaPlatform = temp;
					idColumn = column;
				}
			} catch (IllegalArgumentException e) {
			}
		}

	}

	public TypeMetaPlatform getTypeMetaPlatform() {
		return typeMetaPlatform;
	}

	public int getIdColumn() {
		return idColumn.getPosition().intValue() - 1;
	}

}